<?php

class actChatConvNew extends action
{
	function execute($m)
	{
		// Check key and generate a new one
		$this->checkKey($this->getInput('k')) or $this->doAjaxError();
		$this->setNewKey();
		
		// Get some _GET values
		$persons = $this->GetInput('p') or $this->doAjaxError();
		$channel = $this->getInput('c');
		
		// When no channel variable is given; do error. 
		// We can't use or $this->doAjaxErro() because value zero specifies 'no channel', 
		// but also equals false which triggers the error!
		if ($channel == null) $this->doAjaxError();
		
		// Get the conversation & user model
		$cM = $m->getModel('conversation');
		$uM = $m->getModel('user');
		
		// Set the user model right!
		$uM->setUser(-1);
		
		// Make the persons an array
		$persons = explode(",", $persons);
		
		// If we're not in the persons list yet, add me.
		if (!in_array($uM->getId(), $persons))
			$persons[] = $uM->getId();
		
		// Check if the conversation already exists; if not.. Create it!
		if ($cM->exists($m, $persons, $channel))
		{
			// It exists.
			$this->addOutput('1');
			$this->addOutput($cM->getConversation($m, $persons, $channel));
			
		} // end if
		else
		{
			$this->addOutput('0');
			$this->addOutput($cM->newConversation($m, $persons, $channel));
			
		} // end else
		
		$this->finish();
		
	} // end function
	
} // end class

$_ = 'actChatConvNew';

?>